
# Code to create Figure 2

library(igraph) #user will need to install this package if not already installed

setwd("")  #set working directory to the directory where the replication .zip was unzipped

lit=read.csv("data/LitReview/Data_for_Fig1.csv")

yr=sort(unique(lit$Year))
yr=min(yr):max(yr)
yr=yr[yr!=2013]  #drop 2013
totnum=mmean=mmed=totag=c()
for (i in yr) {
	totnum=c(totnum,sum(lit$Year%in%i))
	totag=c(totag,sum(lit$Year%in%i & lit$Sector=="ag"))
	mmean=c(mmean,mean(lit$number.of.models[lit$Year%in%i]))
	mmed=c(mmed,median(lit$number.of.models[lit$Year%in%i]))
}

#interpolating 1991 when we don't record any studies
mmean[yr==1991]=mean(mmean[yr%in%c(1990,1992)])
mmed[yr==1991]=median(mmed[yr%in%c(1990,1992)])

#calculate 3yr running mean and medians
mn=running.mean(mmean,3)
md=smooth(mmed,kind="3")
md=md[2:(length(md)-1)]  #smooth does not chop off the ends

#cumulative number of studies
cumtot=cumsum(totnum)
cumag=cumsum(totag)

#number of models:  effectively, 15 starting in 2000, 23 in 2005
mods=c(rep(15,5),rep(23,7))

off=0.35
#pdf(file="output/Figure2.pdf",width=10,height=5)
par(mfrow=c(1,2),mar=c(5,4,3,1))
plot(yr,cumtot,type="n",xlab="year",ylab="cumulative studies",las=1)
rect(yr-off,0,yr+off,cumtot,col="grey80",border=NA)
rect(yr-off,0,yr+off,cumag,col="grey50",border=NA)

yrs=1989:2011  #lopped a year off front and back
plot(yrs,mn,ylim=c(0,25),type="l",lty=2,lwd=2,ylab="number of climate models",xlab="year",las=1)
lines(yrs,md,lwd=2,lty=1)
lines(2000:2011,mods,lwd=2,lty=1,col="grey")
text(2010,6.5,"mean",cex=0.8)
text(2010,2,"median",cex=0.8)
text(2009,24,"total available",cex=0.8)
#dev.off()
dev.copy2eps(file="output/Figure2.eps",width=10,height=5)